/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* MainFrame.java
*
* Created on November 7, 2007, 1:52 AM
 */
package gui.genomemap;

import db.genomemap.RDBSettings;
import db.gui.FileToRDBDialog;
import db.gui.RDBConnectionModel;
import db.gui.UpdateSeqInfoDialog;

import fs.genomemap.FileSettings;

import gui.genomemap.outputModel.OutputModel;
import gui.genomemap.settings.FileSettingsDialog;
import gui.genomemap.settings.RDBSettingsDialog;
import gui.genomemap.view.*;

import javautil.swing.FrameUtil;
import javautil.swing.SwingUtil;
import javautil.swing.components.ShortProgressPanel;
import javautil.swing.tabbedPane.MyTabbedPane;

import main.genomemap.Preferences;

import java.awt.*;
import java.io.IOException;
import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;

/**
 *
 * @author  stewari1
 */
public class MainFrame extends javax.swing.JFrame {

    /** Field description */
    private Preferences preferences = null;

    /** Field description */
    private int selectedTabIndex = -1;

    /** Field description */
    private final ShortProgressPanel progressPanel = new ShortProgressPanel();


    // Variables declaration - do not modify

    /** Field description */
    private javax.swing.JMenuItem RDBToFiles_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem about_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem comGLHood_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem comJLHood_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem comPLHood_JMenuItem;

    /** Field description */
    private javax.swing.JMenu dbms_JMenu;

    /** Field description */
    private javax.swing.JMenuItem fileSettings_JMenuItem;

    /** Field description */
    private javax.swing.JMenu file_JMenu;

    /** Field description */
    private javax.swing.JMenuItem filesToRDB_JMenuItem;

    /** Field description */
    private javax.swing.JMenu help_JMenu;

    /** Field description */
    private javax.swing.JEditorPane htmlPane;

    /** Field description */
    private javax.swing.JScrollPane jScrollPane1;

    /** Field description */
    private javax.swing.JMenu jobs_JMenu;

    /** Field description */
    private javax.swing.JLayeredPane layeredPane;

    /** Field description */
    private javax.swing.JMenuItem loadModels_RDB_JMenuItem;

    /** Field description */
    private javax.swing.JMenuBar menuBar;

    /** Field description */
    private javax.swing.JMenuItem preferences_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem progCurve_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem queries_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem rdbSettings_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem saveModels_RDB_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem saveWSpace_JMenuItem;

    /** Field description */
    private javax.swing.JMenu settings_JMenu;

    /** Field description */
    private javax.swing.JMenuItem simAnneal_JMenuItem;

    /** Field description */
    private javax.swing.JPanel startPage_JPanel;

    /** Field description */
    private javax.swing.JMenuItem syncFilesRDB_JMenuItem;

    /** Field description */
    private javautil.swing.tabbedPane.MyTabbedPane tabbedPane;

    /** Field description */
    private javax.swing.JMenuItem updateSeqInfo_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem userManual_JMenuItem;

    /** Field description */
    private javax.swing.JMenuItem xmlModels_JMenuItem;

    /** Creates new form MainFrame */
    private MainFrame() {

        initComponents();
        loadPreferences();
    }

    /**
     * should be called after the frame is set visible
     */
    void addFrameProgressPanel() {
        addProgressPanel(progressPanel);
    }

    /**
     * Method description
     */
    private void loadPreferences() {

        Runnable runnable = new Runnable() {

            public void run() {

                MainFrame.this.publish("Loading Preferences...");

                MainFrame.this.preferences = Preferences.load();

                MainFrame.this.publish("Loading Preferences Finished.");
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane               = new javautil.swing.tabbedPane.MyTabbedPane();
        startPage_JPanel         = new javax.swing.JPanel();
        jScrollPane1             = new javax.swing.JScrollPane();
        htmlPane                 = new javax.swing.JEditorPane();
        layeredPane              = new javax.swing.JLayeredPane();
        menuBar                  = new javax.swing.JMenuBar();
        file_JMenu               = new javax.swing.JMenu();
        loadModels_RDB_JMenuItem = new javax.swing.JMenuItem();
        saveModels_RDB_JMenuItem = new javax.swing.JMenuItem();
        saveWSpace_JMenuItem     = new javax.swing.JMenuItem();
        xmlModels_JMenuItem      = new javax.swing.JMenuItem();
        queries_JMenuItem        = new javax.swing.JMenuItem();
        jobs_JMenu               = new javax.swing.JMenu();
        comGLHood_JMenuItem      = new javax.swing.JMenuItem();
        comPLHood_JMenuItem      = new javax.swing.JMenuItem();
        comJLHood_JMenuItem      = new javax.swing.JMenuItem();
        simAnneal_JMenuItem      = new javax.swing.JMenuItem();
        progCurve_JMenuItem      = new javax.swing.JMenuItem();
        dbms_JMenu               = new javax.swing.JMenu();
        filesToRDB_JMenuItem     = new javax.swing.JMenuItem();
        updateSeqInfo_JMenuItem  = new javax.swing.JMenuItem();
        RDBToFiles_JMenuItem     = new javax.swing.JMenuItem();
        syncFilesRDB_JMenuItem   = new javax.swing.JMenuItem();
        settings_JMenu           = new javax.swing.JMenu();
        preferences_JMenuItem    = new javax.swing.JMenuItem();
        fileSettings_JMenuItem   = new javax.swing.JMenuItem();
        rdbSettings_JMenuItem    = new javax.swing.JMenuItem();
        help_JMenu               = new javax.swing.JMenu();
        userManual_JMenuItem     = new javax.swing.JMenuItem();
        about_JMenuItem          = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {

            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }

        });
        htmlPane.setContentType("text/html");
        htmlPane.setEditable(false);

        try {
            htmlPane.setPage(new java.net.URL("http://www.google.com"));
        } catch (java.io.IOException e1) {
            e1.printStackTrace();
        }

        htmlPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {

            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                htmlPaneHyperlinkUpdate(evt);
            }

        });
        jScrollPane1.setViewportView(htmlPane);

        javax.swing.GroupLayout startPage_JPanelLayout =
            new javax.swing.GroupLayout(startPage_JPanel);

        startPage_JPanel.setLayout(startPage_JPanelLayout);
        startPage_JPanelLayout.setHorizontalGroup(
            startPage_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE));
        startPage_JPanelLayout.setVerticalGroup(
            startPage_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE));
        tabbedPane.addTab("Start Page", startPage_JPanel);
        file_JMenu.setText("File");
        loadModels_RDB_JMenuItem.setText("Load Models From RDB");
        loadModels_RDB_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadModels_RDB_JMenuItemActionPerformed(evt);
            }

        });
        file_JMenu.add(loadModels_RDB_JMenuItem);
        saveModels_RDB_JMenuItem.setText("Save Models to RDB");
        saveModels_RDB_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveModels_RDB_JMenuItemActionPerformed(evt);
            }

        });
        file_JMenu.add(saveModels_RDB_JMenuItem);
        saveWSpace_JMenuItem.setText("Save WorkSpace");
        file_JMenu.add(saveWSpace_JMenuItem);
        xmlModels_JMenuItem.setText("Save to XML Models");
        xmlModels_JMenuItem.setToolTipText(
            "<html>\nThis will create (persisted) XML output models for all<br>\nsaved output models.\n</html>");
        file_JMenu.add(xmlModels_JMenuItem);
        menuBar.add(file_JMenu);
        jobs_JMenu.setText("Jobs");
        comGLHood_JMenuItem.setText("Compute GLHood");
        comGLHood_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comGLHood_JMenuItemActionPerformed(evt);
            }

        });
        jobs_JMenu.add(comGLHood_JMenuItem);
        comPLHood_JMenuItem.setText("Compute PLHood");
        comPLHood_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comPLHood_JMenuItemActionPerformed(evt);
            }

        });
        jobs_JMenu.add(comPLHood_JMenuItem);
        comJLHood_JMenuItem.setText("Compute JLHood");
        comJLHood_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comJLHood_JMenuItemActionPerformed(evt);
            }

        });
        jobs_JMenu.add(comJLHood_JMenuItem);
        simAnneal_JMenuItem.setText("Simulated Annealing");
        simAnneal_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simAnneal_JMenuItemActionPerformed(evt);
            }

        });
        jobs_JMenu.add(simAnneal_JMenuItem);
        progCurve_JMenuItem.setText("Progress Curve");
        progCurve_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                progCurve_JMenuItemActionPerformed(evt);
            }

        });
        jobs_JMenu.add(progCurve_JMenuItem);
        menuBar.add(jobs_JMenu);
        dbms_JMenu.setText("DBMS");
        filesToRDB_JMenuItem.setText("File->RDB");
        filesToRDB_JMenuItem.setToolTipText(
            "<html>\nThis will make a fresh copy of all input data<br>\nand output models from file system to dabase\n</html>");
        filesToRDB_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filesToRDB_JMenuItemActionPerformed(evt);
            }

        });
        dbms_JMenu.add(filesToRDB_JMenuItem);
        updateSeqInfo_JMenuItem.setText("Update Seq Info");
        updateSeqInfo_JMenuItem.setToolTipText(
            "<html>\nThis will update gene and clone/probe sequence information<br>\nfrom <b>FileSystem</b> to an <b>RDBMS</b>\n</html>");
        updateSeqInfo_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateSeqInfo_JMenuItemActionPerformed(evt);
            }

        });
        dbms_JMenu.add(updateSeqInfo_JMenuItem);
        RDBToFiles_JMenuItem.setText("RDB->Files");
        dbms_JMenu.add(RDBToFiles_JMenuItem);
        syncFilesRDB_JMenuItem.setText("Sync Files & RDB");
        syncFilesRDB_JMenuItem.setToolTipText("");
        dbms_JMenu.add(syncFilesRDB_JMenuItem);
        menuBar.add(dbms_JMenu);
        settings_JMenu.setText("Settings");
        preferences_JMenuItem.setText("Preferences");
        settings_JMenu.add(preferences_JMenuItem);
        fileSettings_JMenuItem.setText("File Settings");
        fileSettings_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileSettings_JMenuItemActionPerformed(evt);
            }

        });
        settings_JMenu.add(fileSettings_JMenuItem);
        rdbSettings_JMenuItem.setText("RDB Settings");
        rdbSettings_JMenuItem.setToolTipText("Settings for Relational Database Systems");
        rdbSettings_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbSettings_JMenuItemActionPerformed(evt);
            }

        });
        settings_JMenu.add(rdbSettings_JMenuItem);
        menuBar.add(settings_JMenu);
        help_JMenu.setText("Help");
        userManual_JMenuItem.setText("User Manual");
        userManual_JMenuItem.setToolTipText(
            "<html>\nContains  detailed technical information of the algorithms used<br>\nand information on how to use this software.\n</html>");
        help_JMenu.add(userManual_JMenuItem);
        about_JMenuItem.setText("About");
        about_JMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                about_JMenuItemActionPerformed(evt);
            }

        });
        help_JMenu.add(about_JMenuItem);
        menuBar.add(help_JMenu);
        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 654,
                Short.MAX_VALUE).addComponent(
                    layeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addComponent(
                    tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE).addGap(
                    0, 0, 0).addComponent(
                    layeredPane, javax.swing.GroupLayout.PREFERRED_SIZE, 29,
                    javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, 0)));
        pack();

    }    // </editor-fold>//GEN-END:initComponents

    /**
     * Method description
     *
     * @param evt description
     */
    private void fileSettings_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_fileSettings_JMenuItemActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                FileSettingsDialog dialog = new FileSettingsDialog(new javax.swing.JFrame(), true);

                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_fileSettings_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void updateSeqInfo_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_updateSeqInfo_JMenuItemActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                UpdateSeqInfoDialog dialog = new UpdateSeqInfoDialog(new javax.swing.JFrame(),
                                                 true);


                // since this will have long length put it on top
                int x = (SwingUtil.getScreenWidth() - dialog.getSize().width) / 2;

                dialog.setLocation(x, 0);

                RDBSettings rdbSettings         = RDBSettings.load();
                RDBConnectionModel defaultModel = rdbSettings.getDefaultConnectionModel();

                if (defaultModel != null) {
                    dialog.getRDBConnectionView().setModel(defaultModel);
                }

                dialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_updateSeqInfo_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void htmlPaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {    // GEN-FIRST:event_htmlPaneHyperlinkUpdate

        if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {

            try {

                htmlPane.setPage(evt.getURL());


                // urlField.setText(evt.getURL().toExternalForm());

            } catch (IOException ioe) {

                System.out.println("Can't follow link to " + evt.getURL().toExternalForm() + ": "
                                   + ioe);
            }
        }

    }    // GEN-LAST:event_htmlPaneHyperlinkUpdate

    /**
     * Method description
     *
     * @param evt description
     */
    private void filesToRDB_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_filesToRDB_JMenuItemActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                FileToRDBDialog dialog = new FileToRDBDialog(new javax.swing.JFrame(), true);


                // since this will have long length put it on top
                int x = (SwingUtil.getScreenWidth() - dialog.getSize().width) / 2;

                dialog.setLocation(x, 0);

                RDBSettings rdbSettings         = RDBSettings.load();
                RDBConnectionModel defaultModel = rdbSettings.getDefaultConnectionModel();

                if (defaultModel != null) {
                    dialog.getRDBConnectionView().setModel(defaultModel);
                }

                dialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_filesToRDB_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void rdbSettings_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_rdbSettings_JMenuItemActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                RDBSettingsDialog dialog = new RDBSettingsDialog(new javax.swing.JFrame(), true);

                dialog.setLocationRelativeTo(null);

                RDBSettings rdbSettings         = RDBSettings.load();
                RDBConnectionModel defaultModel = rdbSettings.getDefaultConnectionModel();

                if (defaultModel != null) {
                    dialog.getRDBConnectionView().setModel(defaultModel);
                }

                dialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_rdbSettings_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void progCurve_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_progCurve_JMenuItemActionPerformed


        // add the whole worker view for worker ComGLHood
        ProgCurveView progCurveView = new ProgCurveView();

        addTab(progCurveView);
    }    // GEN-LAST:event_progCurve_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void simAnneal_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_simAnneal_JMenuItemActionPerformed


        // add the whole worker view for worker ComGLHood
        AnnealView annealView = new AnnealView();

        addTab(annealView);
    }    // GEN-LAST:event_simAnneal_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void comJLHood_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_comJLHood_JMenuItemActionPerformed


        // add the whole worker view for worker ComGLHood
        ComJLHoodView comJLHoodView = new ComJLHoodView();

        addTab(comJLHoodView);
    }    // GEN-LAST:event_comJLHood_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void comPLHood_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_comPLHood_JMenuItemActionPerformed


        // add the whole worker view for worker ComGLHood
        ComPLHoodView comPLHoodView = new ComPLHoodView();

        addTab(comPLHoodView);
    }    // GEN-LAST:event_comPLHood_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void about_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_about_JMenuItemActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                AboutDialog aboutDialog = new AboutDialog(new javax.swing.JFrame(), true);

                aboutDialog.setLocationRelativeTo(null);
                aboutDialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_about_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {    // GEN-FIRST:event_tabbedPaneStateChanged

        MyTabbedPane tabpane = (MyTabbedPane) evt.getSource();

        if (tabpane.getSelectedIndex() != selectedTabIndex) {

            selectedTabIndex = tabpane.getSelectedIndex();

            Component comp = tabpane.getComponentAt(selectedTabIndex);

            if (comp instanceof View) {

                View view = (View) comp;

                layeredPane.moveToFront(view.getProgressPanel());
            }
        }

    }    // GEN-LAST:event_tabbedPaneStateChanged

    /**
     * Method description
     *
     * @param evt description
     */
    private void comGLHood_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_comGLHood_JMenuItemActionPerformed


        // add the whole worker view for worker ComGLHood
        ComGLHoodView comGLHoodView = new ComGLHoodView();

        addTab(comGLHoodView);
    }    // GEN-LAST:event_comGLHood_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void loadModels_RDB_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_loadModels_RDB_JMenuItemActionPerformed


        // get all the output models and save them in the file objs thru FileSettings
        EntityManagerFactory emf =
            javax.persistence.Persistence.createEntityManagerFactory("OutputModels");
        EntityManager em          = emf.createEntityManager();
        FileSettings fileSettings = FileSettings.load();

        em.getTransaction().begin();

        try {

            String[] outputModelNames = OutputModel.getOutputModelNames();

            for (String outputModel : outputModelNames) {

                fileSettings.saveOutputModels(
                    em.createQuery("SELECT c FROM :model").setParameter(
                        "model", outputModel).getResultList());
            }

            em.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();
            em.getTransaction().rollback();

        } finally {
            em.close();
        }
    }    // GEN-LAST:event_loadModels_RDB_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void saveModels_RDB_JMenuItemActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_saveModels_RDB_JMenuItemActionPerformed


        // get all the output models from the file objs
        // thru FileSettings and save(persist) them in the DB
        EntityManagerFactory emf =
            javax.persistence.Persistence.createEntityManagerFactory("OutputModels");

        System.out.println("emf null: " + emf == null);

        EntityManager em          = emf.createEntityManager();
        FileSettings fileSettings = FileSettings.load();

        em.getTransaction().begin();

        try {

            Class[] outputModelClasses = OutputModel.getOutputModelClasses();

            for (Class cls : outputModelClasses) {

                Collection<OutputModel> outputModels = fileSettings.loadOutputModelCollection(cls);

                for (OutputModel outputModel : outputModels) {

                    em.persist(outputModel);
                }
            }

            em.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();
            em.getTransaction().rollback();

        } finally {
            em.close();
        }
    }    // GEN-LAST:event_saveModels_RDB_JMenuItemActionPerformed

    /**
     * Method description
     *
     * @param view description
     */
    private void addTab(View view) {

        tabbedPane.add(view.getDescription(), view);
        tabbedPane.setSelectedIndex(tabbedPane.indexOfComponent(view));
        addProgressPanel(view.getProgressPanel());
        layeredPane.moveToFront(view.getProgressPanel());
    }

    /**
     * Method description
     *
     * @param progressPanel description
     */
    private void addProgressPanel(ShortProgressPanel progressPanel) {

        progressPanel.setLocation(0, 0);

        Dimension dimension = layeredPane.getSize();

        progressPanel.setSize(new Dimension(dimension.width - 1, dimension.height - 1));
        progressPanel.setStatusInfo("");
        progressPanel.setShowProgress(false);
        layeredPane.add(progressPanel, JLayeredPane.DEFAULT_LAYER);
    }

    /**
     * Method description
     *
     * @param str description
     */
    private void publish(String str) {

        layeredPane.moveToFront(progressPanel);
        progressPanel.setStatusInfo(str);
    }

    /**
     * Method description
     *
     * @param progress description
     */
    private void setProgress(int progress) {

        layeredPane.moveToFront(progressPanel);
        progressPanel.setProgress(progress);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                         | UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }

                MainFrame frame = new MainFrame();

                FrameUtil.centralize(frame, 40);
                frame.setVisible(true);
                frame.addFrameProgressPanel();
            }

        });
    }


    // End of variables declaration
    public void persist(Object object) {

        EntityManagerFactory emf =
            javax.persistence.Persistence.createEntityManagerFactory("OutputModels");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        try {

            em.persist(object);
            em.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();
            em.getTransaction().rollback();

        } finally {
            em.close();
        }
    }
}
